package cn.com.bluemoon.daps.model.entity;

import cn.com.bluemoon.daps.common.domain.BaseModel;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.sql.IDbDataSource;
import cn.com.bluemoon.daps.common.validation.illegal.IllegalChar;
import cn.com.bluemoon.daps.model.dto.DapModelVersionInter;
import com.baomidou.mybatisplus.annotation.TableField;
import com.google.common.base.Objects;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * <p>
 * 数据模型-模型中设计表-分区表
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Data
@Accessors
@ApiModel(value = "DapDataModelTablePartition", description = "数据模型-模型中设计表-分区表")
public class DapDataModelTablePartition extends BaseModel<DapDataModelTablePartition>
        implements DapModelVersionInter, IDbDataSource {

    /**
     * 分区字段类型默认值
     */
    public static final Integer PARTITION_FIELD_TYPE_DEFAULT_KEY = 1;

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "关联模型id")
    @NotNull(message = "关联模型id不允许为空")
    private String dataModelId;

    @ApiModelProperty(value = "数据模型-模型表id")
    @NotNull(message = "关联数据模型-模型表id不允许为空")
    private String modelTableId;

    @ApiModelProperty(value = "分区字段")
    @NotNull(message = "分区字段不允许为空")
    @Size(max = 64, message = "分区字段不可以大于{max}")
    @IllegalChar(name = "partitionFieldName", message = "字段名中存在非法字符")
    // @NoSqlKeyword(name = "partitionFieldName", message = "字段名不可为关键字")
    private String partitionFieldName;

    @ApiModelProperty(value = "分区字段类型, 1 String 2 Integer")
    @NotNull(message = "分区字段类型不允许为空")
    private Integer partitionFieldType = PARTITION_FIELD_TYPE_DEFAULT_KEY;

    @ApiModelProperty(value = "分区名")
    @Size(max = 64, message = "分区名不可以大于{max}")
    private String name;

    @ApiModelProperty(value = "已发布版本的数据，记录模型下最新的唯一id，比如：最新表id,最新索引id ")
    private String pId;

    @ApiModelProperty(value = "标记当前数据是否为模型最新数据，null/0 为已发布数据、1为该模型下的最新数据")
    private Integer dataNewest;

    /**
     * 用于查询字段+表名的对象
     */
    @ApiModelProperty(value = "表英文名称", hidden = true)
    @TableField(exist = false)
    private String tableName;

    @ApiModelProperty(value = "用于数据对比中多版本的关联键值", hidden = true)
    @TableField(exist = false)
    private String bmKey;
    @ApiModelProperty(value = "数据库类型，用于区分不同数据库，equals中mysql忽略大小写判断相等", hidden = true)
    @TableField(exist = false)
    private DatabaseType dbType;

    public DapDataModelTablePartition() {
        this.dbType = DatabaseType.MYSQL;
    }

    public DapDataModelTablePartition(DatabaseType dbType) {
        this.dbType = dbType;
    }

    @Override
    public boolean equals(Object o) {
        //noinspection AliControlFlowStatementWithoutBraces
        if (this == o) {
            return true;
        }
        //noinspection AliControlFlowStatementWithoutBraces
        if (!(o instanceof DapDataModelTablePartition)) {
            return false;
        }
        //noinspection AliControlFlowStatementWithoutBraces
        if (!super.equals(o)) {
            return false;
        }
        DapDataModelTablePartition partition = (DapDataModelTablePartition) o;
        if (DatabaseType.MYSQL.equals(dbType)){
            return StringUtils.equalsIgnoreCase(partitionFieldName, partition.partitionFieldName) &&
                   StringUtils.equalsIgnoreCase(name, partition.name);
        }
        return Objects.equal(super.getId(), partition.getId()) &&
                Objects.equal(name, partition.name);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(super.hashCode(), super.getId(), name);
    }
}
